function kondo3d (){
	var that = this;
	that.container;
	that.stats;
	that.camera; 
	that.scene; 
	that.renderer;
	that.targetRotationX = 0;
	that.targetRotationXOnMouseDown = 0;
	that.targetRotationY = 0;
	that.targetRotationYOnMouseDown = 0;
	that.mouseX = 0;
	that.mouseXOnMouseDown = 0;
	that.mouseY = 0;
	that.mouseYOnMouseDown = 0;
	that.mult = 10;
	that.servosPos = {};
	that.withStat = false;
	that.mousepressed = false;
	that.kinetic = {
		id: "c0", x: 0, y:  0, z:0, rx:0	, ry:0, rz:  0, color : 0xa0a0a0,
		childs : [{
			id: "a0", x: 0, y:  +6, z:0, rx:0	, ry:0, rz:  0, color : 0xa0aa,
		},{
			id: "r1", x: 3, y:  4, z:0, rx:0	, ry:0, rz: 90, color : 0xff0000,
			childs : [{
				id: "r2", x: +3, y:  -1, z:0, rx:0	, ry:0, rz:180, color : 0xa0aa,
				childs : [{
					id: "r4", x: 0, y: -4, z:0, rx:0	, ry:0, rz:180,color : 0x444444
				}]
			}]
		},{
			id: "l1", x: -3, y:  4, z:0, rx:0	, ry:0, rz: 90, color : 0xff0000,
			childs : [{
				id: "l2", x: -3, y:  -1, z:0, rx:0	, ry:0, rz:180, color : 0xa0aa,
				childs : [{
					id: "l4", x: 0, y: -4, z:0, rx:0	, ry:0, rz:180, color : 0x444444
				}]
			}]
		},{
			id: "r6", x: 2, y: -3, z:0, rx:0	, ry:0, rz: 90, color : 0x0aa,
			childs:[{
				id: "r7", x: 0, y: -3, z:0, rx:0	, ry:0, rz:180, color : 0x000011,
				childs:[{
					id: "r8", x: 0, y: -4, z:0, rx:0	, ry:0, rz:180, color : 0x000011,
					childs:[{
						id: "r9", x: 1, y: -3, z:0, rx:0	, ry:0, rz:90, color : 0x444444
					}]
				}]
			}]
		},{
			id: "l6", x: -2, y: -3, z:0, rx:0	, ry:0, rz: 90,label:"",color : 0x0aa,
			childs:[{
				id: "l7", x: 0, y: -3, z:0, rx:0	, ry:0, rz:180,label:"",color : 0x000011,
				childs:[{
					id: "l8", x: 0, y: -4, z:0, rx:0	, ry:0, rz:180,label:"",color : 0x000011,
					childs:[{
						id: "l9", x: -1, y: -3, z:0, rx:0	, ry:0, rz:90,label:"",color : 0x444444
					}]
				}]
			}]
		}]
	};
				
	
	that.kondo3dLoop = function() {
		that.targetRotationX = that.targetRotationX+ 0.02;
		that.camera.position.x= Math.cos(that.targetRotationX)*150;
		that.camera.position.z= Math.sin(that.targetRotationX)*150;
		that.camera.position.y= Math.sin(that.targetRotationY)*150;
		that.renderer.render(that.scene, that.camera);
		if (that.withStat){
			that.stats.update();
		}
	}
	
	that.addServos = function (delta,config){
		var geometry = new Cube( 1*that.mult, 1.5*that.mult, 1*that.mult );
		for (var i = 0; i < geometry.faces.length; i++) {
			geometry.faces[i].material = [ new THREE.MeshColorFillMaterial( config.color+Math.random()*10, 1 ) ];
		}
		cube = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial() );
		delta += that.servosPos[config.id]?that.servosPos[config.id]:0;
		config.rz +=delta;
		cube.servoID 		= config.id;
		cube.position.x 	= config. x*5-that.mult/2;
		cube.position.y 	= config. y*5-that.mult/2;
		cube.position.z 	= config. z*5-that.mult/2;
		cube.rotation.x		= config.rx*3.14*2/360;
		cube.rotation.y		= config.ry*3.14*2/360;
		cube.rotation.z		= config.rz*3.14*2/360;
		that.scene.addObject(cube);
		if (config.childs){
			for (var k in config.childs){
				var child=config.childs[k];
				that.addServos(delta,{
					id		: child.id,
					x		: child. x + config. x + (delta?Math.cos(delta)*1:0),
					y		: child. y + config. y + (delta?Math.sin(delta)*1:0),
					z		: child. z + config. z,
					rx		: child.rx,
					ry		: child.ry,
					rz		: child.rz+delta,
					childs	: child.childs?child.childs:null,
					color	: child.color ?child.color :null								
				});
			}
		}
	}
	that.init = function(config) {
		for (var cfgKey in config){
			this[cfgKey]=config[cfgKey];			
		}
		that.windowHalfX = that.container.innerWidth() / 2;
		that.windowHalfY = that.container.innerHeight() / 2;
		that.camera = new THREE.Camera( 70, that.container.innerWidth() / that.container.innerHeight(), 1, 10000 );
		that.camera.position.x = 0;
		that.camera.position.y = -40;
		that.camera.position.z = 0;
		that.camera.target.position.x = 0;
		that.camera.target.position.y = -40;
		that.camera.target.position.z = 0;
	
		that.scene = new THREE.Scene();
		that.servosPos = {
			"l6":-10,
			"l8":-20 
		};

		that.addServos(0,that.kinetic);
		that.renderer = new THREE.CanvasRenderer();
		that.renderer.setSize(that.container.innerWidth(), that.container.innerHeight());
		that.container.append( that.renderer.domElement );
	
		if (that.withStat){
			that.stats = new Stats();
			that.stats.domElement.style.position = 'absolute';
			that.stats.domElement.style.top = '0px';
			that.container.append( that.stats.domElement );
		}
	
		that.container.mousedown(that.onMouseDown);
		that.container[0].addEventListener( 'touchstart', that.onTouchStart, false );
		that.container[0].addEventListener( 'touchmove' , that.onTouchMove , false );
		setInterval(that.kondo3dLoop, 1000/60);
	}
	
	that.onMouseDown =function( event ) {
		event.preventDefault();
		that.mousepressed = true;
		that.container.mousemove(that.onMouseMove);
		that.container.mouseup	(that.onMouseUp	 );
		that.container.mouseout	(that.onMouseOut );
		that.mouseXOnMouseDown = event.clientX - that.windowHalfX;
		that.mouseYOnMouseDown = event.clientY - that.windowHalfY;
		that.targetRotationXOnMouseDown = that.targetRotationX;
		that.targetRotationYOnMouseDown = that.targetRotationY;
	}
	
	that.onMouseMove =function( event ) {
		if (that.mousepressed){
			var mouseX = event.clientX - that.windowHalfX;
			var mouseY = event.clientY - that.windowHalfY;
			that.targetRotationX = that.targetRotationXOnMouseDown + ( mouseX - that.mouseXOnMouseDown ) * 0.02;
			that.targetRotationY = that.targetRotationYOnMouseDown + ( mouseY - that.mouseYOnMouseDown ) * 0.02;
		}
	}
	
	that.onMouseUp =function ( event ) {
		that.mousepressed = false;
		that.container
			.mousemove(that.onMouseMove)
			.mouseup  (that.onMouseUp  )
			.mouseout (that.onMouseOut );
	}
	
	that.onMouseOut=function( event ) {
		that.mousepressed = false;
		that.container
			.mousemove(that.onMouseMove)
			.mouseup  (that.onMouseUp  )
			.mouseout (that.onMouseOut );
	}
	
	that.onTouchStart =function( event ) {
		if ( event.touches.length == 1 ) {
			event.preventDefault();
			that.mouseXOnMouseDown = event.touches[ 0 ].pageX - that.windowHalfX;
			that.mouseYOnMouseDown = event.touches[ 0 ].pageY - that.windowHalfY;
			that.targetRotationXOnMouseDown = that.targetRotationX;
			that.targetRotationYOnMouseDown = that.targetRotationY;
		}
	}
	
	that.onTouchMove =function( event ) {
		if ( event.touches.length == 1 ) {
			event.preventDefault();
			var mouseX = event.touches[ 0 ].pageX - windowHalfX;
			var mouseY = event.touches[ 0 ].pageY - windowHalfY;
			that.targetRotationX = that.targetRotationXOnMouseDown + ( mouseX - that.mouseXOnMouseDown ) * 0.05;
			that.targetRotationY = that.targetRotationYOnMouseDown + ( mouseY - that.mouseXOnMouseDown ) * 0.05;
		}
	}
}
